#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#include "proto.h"


int main(void)
{

    int tcp_socket, accepted_socket;
    struct sockaddr_in laddr;
    struct msg_st buf;
    size_t ret;

    tcp_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (-1 == tcp_socket) {
        perror("socket()");
        exit(1);
    }

    laddr.sin_family = AF_INET;
    inet_aton(SERVER_IP, &laddr.sin_addr);
    laddr.sin_port = htons(SERVER_PORT);

    if (-1 == connect(tcp_socket, (struct sockaddr *)&laddr, sizeof(laddr))) {
        perror("connect()");
        goto ERROR;
    }

    ret = recv(tcp_socket, buf.data, MAXMSG, 0);

    if (-1 == ret) {
        perror("recv()");
        goto ERROR;
    }

    printf("msg:%s\n", buf.data);

    close(tcp_socket);

    return 0;
ERROR:
    close(tcp_socket);
    exit(1);
}

